package net.huashitong.Indicators.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.Indicators.controller.UserInfoController;
import net.huashitong.Indicators.dataBean.CodeDataBean;
import net.huashitong.Indicators.entity.AreaCode;
import net.huashitong.Indicators.entity.IndexData;
import net.huashitong.Indicators.entity.OneIndex;
import net.huashitong.Indicators.entity.UserInfo;
import net.huashitong.until.Until;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.*;

import static net.huashitong.Indicators.utils.DataFormateUntil.removeDuplicate;
import static net.huashitong.until.Until.*;
import static net.huashitong.until.Until.NIANDU_SHIJI;
import static net.huashitong.until.Until.YUEDU_SHIJI;

@Service
public class ClocksService {

    @Autowired
    private AreaCodeService areaService;

    @Autowired
    private IndexDataService indexservice;

    @Autowired
    private OneIndexService oneService;

    @Autowired
    private UserInfoService service;

    @Autowired
    private GetDataService getDataService;


    private static Logger log = LoggerFactory.getLogger(ClocksService.class);

    /**
     * A 区级 B 四个特区，C 十五个副省级 D 市级数据，E 九地市  G 全国  P 全省
     * 季度调度任务
     */
    public void quarterData() {
        log.info("----------------------------计时器开始: " + Until.getCurrentDateTime() + "-------------------------------");

        //Y月度 J 季度 N 年度
        // A 区级 B 四个特区，C 十五个副省级 D 市级数据，E 九地市  G 全国  P 全省
        /*//1.更新地区
        this.savaAreaCode();*/
        //封装地区map
        List<AreaCode> areaCodeList = areaService.query();
        Map<String, String> areaMap = new HashMap<>();
        for (AreaCode areaCode : areaCodeList) {
            if (!areaMap.containsKey(areaCode.getAreaCode())) {
                areaMap.put(areaCode.getAreaCode(), areaCode.getAreaName());
            }
        }
        Map<String, Object> listmap;

        log.info("----------------------------计时器开始: " + Until.getCurrentDateTime() + "-------------------------------");
        log.info("--------------------------区级任务开始: " + Until.getCurrentDateTime() + "-------------------------------");
        log.info("--------区级别数据 季度 " + Until.getCurrentDateTime() + "--------");
        String codetype = "J";
        String areaType = "A";
//        listmap = getYueDuDataFromURL(Until.JIDU_QUJI, codetype, areaType);
//        saveIndexs(listmap, codetype, areaType);
//        updateIndexInfo(Until.JIDU_QUJI, codetype);
        saveNianDuData(Until.JIDU_QUJI, codetype, areaType, areaMap);
        log.info("---------区级别数据季度数据保存成功 " + Until.getCurrentDateTime());
        log.info("--------------------------区级任务结束: " + Until.getCurrentDateTime() + "-------------------------------");
        log.info("--------------------------四个经济特区任务开始: " + Until.getCurrentDateTime() + "-------------------------------");
        log.info("-------四个经济特区 季度 " + Until.getCurrentDateTime() + "------------");
        codetype = "J";
        areaType = "B";
//        listmap = getYueDuDataFromURL(Until.JIDU_TEQU4, codetype, areaType);
//        saveIndexs(listmap, codetype, areaType);
//        updateIndexInfo(Until.JIDU_TEQU4, codetype);
        saveNianDuData(Until.JIDU_TEQU4, codetype, areaType, areaMap);
        log.info("--------------------------四个经济特区任务结束: " + Until.getCurrentDateTime() + "-------------------------------");
        log.info("--------------------------十五个副省级任务开始: " + Until.getCurrentDateTime() + "-------------------------------");
        log.info("---------十五个副省级 季度" + Until.getCurrentDateTime() + "-----------");
        codetype = "J";
        areaType = "C";
//        listmap = getYueDuDataFromURL(Until.JIDU_FUSHENGJI15, codetype, areaType);
//        saveIndexs(listmap, codetype, areaType);
//        updateIndexInfo(Until.JIDU_FUSHENGJI15, codetype);
        saveNianDuData(Until.JIDU_FUSHENGJI15, codetype, areaType, areaMap);
        log.info("---------十五个副省级季度数据保存成功 " + Until.getCurrentDateTime());
        log.info("--------------------------十五个副省级任务结束: " + Until.getCurrentDateTime() + "-------------------------------");
        log.info("--------------------------市级任务开始: " + Until.getCurrentDateTime() + "-------------------------------");
        log.info("---------市级 季度" + Until.getCurrentDateTime() + "-----------");
        areaType = "D";
        codetype = "J";
//        listmap = getYueDuDataFromURL(Until.JIDU_SHIJI, codetype, areaType);
//        saveIndexs(listmap, codetype, areaType);
//        updateIndexInfo(Until.JIDU_SHIJI, codetype);
        saveYueDuData(Until.JIDU_SHIJI, codetype, areaType);
        log.info("---------季度数据保存成功 " + Until.getCurrentDateTime());
        log.info("--------------------------市级任务结束: " + Until.getCurrentDateTime() + "-------------------------------");
        log.info("--------------------------九地市开始: " + Until.getCurrentDateTime() + "-------------------------------");
        log.info("--------九地市 季度 " + Until.getCurrentDateTime() + "------------");
        codetype = "J";
        areaType = "E";
//        listmap = getYueDuDataFromURL(Until.JIDU_9DI, codetype, areaType);
//        saveIndexs(listmap, codetype, areaType);
//        updateIndexInfo(Until.JIDU_9DI, codetype);
        saveNianDuData(Until.JIDU_9DI, codetype, areaType, areaMap);
        log.info("---------九地市季度数据保存成功 " + Until.getCurrentDateTime());
        log.info("--------------------------九地市结束: " + Until.getCurrentDateTime() + "-------------------------------");
        log.info("--------------------------全国任务开始: " + Until.getCurrentDateTime() + "-------------------------------");
        log.info("-------全国 季度 " + Until.getCurrentDateTime() + "------------");
        codetype = "J";
        areaType = "G";
//        listmap = getYueDuDataFromURL(Until.JIDU_QUANGUO, codetype, areaType);
//        saveIndexs(listmap, codetype, areaType);
//        updateIndexInfo(Until.JIDU_QUANGUO, codetype);
        saveNianDuData(Until.JIDU_QUANGUO, codetype, areaType, areaMap);
        log.info("---------全国季度保存成功 " + Until.getCurrentDateTime());
        log.info("--------------------------全国任务结束: " + Until.getCurrentDateTime() + "-------------------------------");
        log.info("--------------------------全省任务开始: " + Until.getCurrentDateTime() + "-------------------------------");
        log.info("-------全省 季度 " + Until.getCurrentDateTime() + "------------");
        codetype = "J";
        areaType = "P";
//        listmap = getYueDuDataFromURL(Until.JIDU_QS, codetype, areaType);
//        saveIndexs(listmap, codetype, areaType);
//        updateIndexInfo(Until.JIDU_QS, codetype);
        saveNianDuData(Until.JIDU_QS, codetype, areaType, areaMap);
        log.info("---------全省季度保存成功 " + Until.getCurrentDateTime());
        log.info("--------------------------全省任务结束: " + Until.getCurrentDateTime() + "-------------------------------");

//        log.info("---------首页 月度 " + Until.getCurrentDateTime() + "------------");
//        areaType = "S";
//        codetype = "Y";
////        Map<String, Object> listmap = getYueDuDataFromURL(Until.YEUDU_SHOUYE, codetype, areaType);
////        saveIndexs(listmap, codetype, areaType);
////        updateIndexInfo(Until.YEUDU_SHOUYE,codetype);
////        saveNianDuData(Until.YEUDU_SHOUYE, codetype, areaType, areaMap);
//        saveYueDuData(Until.YEUDU_SHOUYE, codetype, areaType);
//        log.info("---------首页 月度更新成功" + Until.getCurrentDateTime() + "------------");

        log.info("--------------------------计时器结束： " + Until.getCurrentDateTime() + "-------------------------------");

    }


    /**
     * 区级季度（所有时间）
     */
    public void quarterDataAllTimeA() {
        //封装地区map
        List<AreaCode> areaCodeList = areaService.query();
        Map<String, String> areaMap = new HashMap<>();
        for (AreaCode areaCode : areaCodeList) {
            if (!areaMap.containsKey(areaCode.getAreaCode())) {
                areaMap.put(areaCode.getAreaCode(), areaCode.getAreaName());
            }
        }
        log.info("----------------------------计时器开始: " + Until.getCurrentDateTime() + "-------------------------------");
        log.info("--------------------------区级任务开始: " + Until.getCurrentDateTime() + "-------------------------------");
        log.info("--------区级别数据 季度 " + Until.getCurrentDateTime() + "--------");
        String codetype = "J";
        String areaType = "A";
        //        listmap = getYueDuDataFromURL(Until.JIDU_QUJI, codetype, areaType);
//        saveIndexs(listmap, codetype, areaType);
//        updateIndexInfo(Until.JIDU_QUJI, codetype);
        saveNianDuData(Until.JIDU_QUJI, codetype, areaType, areaMap);
        log.info("---------区级别数据季度数据保存成功 " + Until.getCurrentDateTime());
        log.info("--------------------------区级任务结束: " + Until.getCurrentDateTime() + "-------------------------------");
    }

    /**
     * 四个经济特区季度（所有时间）
     */
    public void quarterDataAllTimeB() {
        //封装地区map
        List<AreaCode> areaCodeList = areaService.query();
        Map<String, String> areaMap = new HashMap<>();
        for (AreaCode areaCode : areaCodeList) {
            if (!areaMap.containsKey(areaCode.getAreaCode())) {
                areaMap.put(areaCode.getAreaCode(), areaCode.getAreaName());
            }
        }
        log.info("----------------------------计时器开始: " + Until.getCurrentDateTime() + "-------------------------------");
        log.info("--------------------------四个经济特区任务开始: " + Until.getCurrentDateTime() + "-------------------------------");
        log.info("-------四个经济特区 季度 " + Until.getCurrentDateTime() + "------------");
        String codetype = "J";
        String areaType = "B";
//        listmap = getYueDuDataFromURL(Until.JIDU_TEQU4, codetype, areaType);
//        saveIndexs(listmap, codetype, areaType);
//        updateIndexInfo(Until.JIDU_TEQU4, codetype);
        saveNianDuData(Until.JIDU_TEQU4, codetype, areaType, areaMap);
        log.info("---------四个经济特区季度数据保存成功 " + Until.getCurrentDateTime());
        log.info("--------------------------四个经济特区任务结束: " + Until.getCurrentDateTime() + "-------------------------------");
    }

    /**
     * 十五个副省级季度（所有时间）
     */
    public void quarterDataAllTimeC() {
        //封装地区map
        List<AreaCode> areaCodeList = areaService.query();
        Map<String, String> areaMap = new HashMap<>();
        for (AreaCode areaCode : areaCodeList) {
            if (!areaMap.containsKey(areaCode.getAreaCode())) {
                areaMap.put(areaCode.getAreaCode(), areaCode.getAreaName());
            }
        }
        log.info("--------------------------十五个副省级任务开始: " + Until.getCurrentDateTime() + "-------------------------------");
        log.info("---------十五个副省级 季度" + Until.getCurrentDateTime() + "-----------");
        String codetype = "J";
        String areaType = "C";
//        listmap = getYueDuDataFromURL(Until.JIDU_FUSHENGJI15, codetype, areaType);
//        saveIndexs(listmap, codetype, areaType);
//        updateIndexInfo(Until.JIDU_FUSHENGJI15, codetype);
        saveNianDuData(Until.JIDU_FUSHENGJI15, codetype, areaType, areaMap);
        log.info("---------十五个副省级季度数据保存成功 " + Until.getCurrentDateTime());
        log.info("--------------------------十五个副省级任务结束: " + Until.getCurrentDateTime() + "-------------------------------");
    }

    /**
     * 市级季度（所有时间）
     */
    public void quarterDataAllTimeD() {
        log.info("--------------------------市级任务开始: " + Until.getCurrentDateTime() + "-------------------------------");
        log.info("---------市级 季度" + Until.getCurrentDateTime() + "-----------");
        String areaType = "D";
        String codetype = "J";
//        listmap = getYueDuDataFromURL(Until.JIDU_SHIJI, codetype, areaType);
//        saveIndexs(listmap, codetype, areaType);
//        updateIndexInfo(Until.JIDU_SHIJI, codetype);
        saveYueDuData(Until.JIDU_SHIJI, codetype, areaType);
        log.info("---------季度数据保存成功 " + Until.getCurrentDateTime());
        log.info("--------------------------市级任务结束: " + Until.getCurrentDateTime() + "-------------------------------");
    }

    /**
     * 全省季度（所有时间）
     */
    public void quarterDataAllTimeP() {
        log.info("--------------------------全省任务开始: " + Until.getCurrentDateTime() + "-------------------------------");
        log.info("---------全省 季度" + Until.getCurrentDateTime() + "-----------");
        String areaType = "P";
        String codetype = "J";
//        listmap = getYueDuDataFromURL(Until.JIDU_QS, codetype, areaType);
//        saveIndexs(listmap, codetype, areaType);
//        updateIndexInfo(Until.JIDU_QS, codetype);
        saveYueDuData(Until.JIDU_QS, codetype, areaType);
        log.info("---------全省季度数据保存成功 " + Until.getCurrentDateTime());
        log.info("--------------------------全省任务结束: " + Until.getCurrentDateTime() + "-------------------------------");
    }

    /**
     * 九地市季度（所有时间）
     */
    public void quarterDataAllTimeE() {
        //封装地区map
        List<AreaCode> areaCodeList = areaService.query();
        Map<String, String> areaMap = new HashMap<>();
        for (AreaCode areaCode : areaCodeList) {
            if (!areaMap.containsKey(areaCode.getAreaCode())) {
                areaMap.put(areaCode.getAreaCode(), areaCode.getAreaName());
            }
        }
        log.info("--------------------------九地市开始: " + Until.getCurrentDateTime() + "-------------------------------");
        log.info("--------九地市 季度 " + Until.getCurrentDateTime() + "------------");
        String codetype = "J";
        String areaType = "E";
//        listmap = getYueDuDataFromURL(Until.JIDU_9DI, codetype, areaType);
//        saveIndexs(listmap, codetype, areaType);
//        updateIndexInfo(Until.JIDU_9DI, codetype);
        saveNianDuData(Until.JIDU_9DI, codetype, areaType, areaMap);
        log.info("---------九地市季度数据保存成功 " + Until.getCurrentDateTime());
        log.info("--------------------------九地市结束: " + Until.getCurrentDateTime() + "-------------------------------");
    }

    /**
     * 全国季度（所有时间）
     */
    public void quarterDataAllTimeG() {
        //封装地区map
        List<AreaCode> areaCodeList = areaService.query();
        Map<String, String> areaMap = new HashMap<>();
        for (AreaCode areaCode : areaCodeList) {
            if (!areaMap.containsKey(areaCode.getAreaCode())) {
                areaMap.put(areaCode.getAreaCode(), areaCode.getAreaName());
            }
        }
        log.info("--------------------------全国任务开始: " + Until.getCurrentDateTime() + "-------------------------------");
        log.info("-------全国 季度 " + Until.getCurrentDateTime() + "------------");
        String codetype = "J";
        String areaType = "G";
//        listmap = getYueDuDataFromURL(Until.JIDU_QUANGUO, codetype, areaType);
//        saveIndexs(listmap, codetype, areaType);
//        updateIndexInfo(Until.JIDU_QUANGUO, codetype);
        saveNianDuData(Until.JIDU_QUANGUO, codetype, areaType, areaMap);
        log.info("---------全国季度保存成功 " + Until.getCurrentDateTime());
        log.info("--------------------------全国任务结束: " + Until.getCurrentDateTime() + "-------------------------------");
    }


    /**
     * 年度调度任务
     */
    public void yearData() {
        log.info("----------------------------计时器开始: " + Until.getCurrentDateTime() + "-------------------------------");
        //Y月度 J 季度 N 年度
        // A 区级 B 四个特区，C 十五个副省级 D 市级数据，E 九地市  G 全国
        /*//1.更新地区
        this.savaAreaCode();*/
        //封装地区map
        List<AreaCode> areaCodeList = areaService.query();
        Map<String, String> areaMap = new HashMap<>();
        for (AreaCode areaCode : areaCodeList) {
            if (!areaMap.containsKey(areaCode.getAreaCode())) {
                areaMap.put(areaCode.getAreaCode(), areaCode.getAreaName());
            }
        }
        String codetype;
        String areaType;
        Map<String, Object> listmap;
        System.out.println(Until.getCurrentDateTime());
        log.info("----------------------------计时器开始: " + Until.getCurrentDateTime() + "-------------------------------");
        log.info("--------------------------全省任务开始: " + Until.getCurrentDateTime() + "-------------------------------");
        log.info("---------全省 年度" + Until.getCurrentDateTime() + "-----------");
        areaType = "P";
        codetype = "N";
//        listmap = getYueDuDataFromURL(Until.NIANDU_QS, codetype, areaType);
//        saveIndexs(listmap, codetype, areaType);
//        updateIndexInfo(Until.NIANDU_QS, codetype);
        saveYueDuData(Until.NIANDU_QS, codetype, areaType);
        log.info("---------年度数据保存成功 " + Until.getCurrentDateTime());
        log.info("--------------------------全省任务结束: " + Until.getCurrentDateTime() + "-------------------------------");
        log.info("--------------------------区级任务开始: " + Until.getCurrentDateTime() + "-------------------------------");
        log.info("--------区级别数据 年度 " + Until.getCurrentDateTime() + "--------");
        codetype = "N";
        areaType = "A";
//        listmap = getYueDuDataFromURL(Until.NIANDU_QUJI, codetype, areaType);
//        saveIndexs(listmap, codetype, areaType);
//        updateIndexInfo(Until.NIANDU_QUJI, codetype);
        saveNianDuData(Until.NIANDU_QUJI, codetype, areaType, areaMap);
        log.info("---------区级别数据年度数据保存成功 " + Until.getCurrentDateTime());
        log.info("--------------------------区级任务结束: " + Until.getCurrentDateTime() + "-------------------------------");
        log.info("--------------------------四个经济特区任务开始: " + Until.getCurrentDateTime() + "-------------------------------");
        log.info("-------四个经济特区 年度 " + Until.getCurrentDateTime() + "------------");
        codetype = "N";
        areaType = "B";
//        listmap = getYueDuDataFromURL(Until.NIANDU_TEQU4, codetype, areaType);
//        saveIndexs(listmap, codetype, areaType);
//        updateIndexInfo(Until.NIANDU_TEQU4, codetype);
        saveNianDuData(Until.NIANDU_TEQU4, codetype, areaType, areaMap);
        log.info("--------------------------四个经济特区任务结束: " + Until.getCurrentDateTime() + "-------------------------------");
        log.info("--------------------------十五个副省级任务开始: " + Until.getCurrentDateTime() + "-------------------------------");
        log.info("---------十五个副省级 年度" + Until.getCurrentDateTime() + "-----------");
        codetype = "N";
        areaType = "C";
//        listmap = getYueDuDataFromURL(Until.NIANDU_FUSHENGJI15, codetype, areaType);
//        saveIndexs(listmap, codetype, areaType);
//        updateIndexInfo(Until.NIANDU_FUSHENGJI15, codetype);
        saveNianDuData(Until.NIANDU_FUSHENGJI15, codetype, areaType, areaMap);
        log.info("---------十五个副省级 年度数据保存成功 " + Until.getCurrentDateTime());
        log.info("--------------------------十五个副省级任务结束: " + Until.getCurrentDateTime() + "-------------------------------");
        log.info("--------------------------市级任务开始: " + Until.getCurrentDateTime() + "-------------------------------");
        log.info("---------市级 年度" + Until.getCurrentDateTime() + "-----------");
        areaType = "D";
        codetype = "N";
//        listmap = getYueDuDataFromURL(Until.NIANDU_SHIJI, codetype, areaType);
//        saveIndexs(listmap, codetype, areaType);
//        updateIndexInfo(Until.NIANDU_SHIJI, codetype);
        saveYueDuData(Until.NIANDU_SHIJI, codetype, areaType);
        log.info("---------年度数据保存成功 " + Until.getCurrentDateTime());
        log.info("--------------------------市级任务结束: " + Until.getCurrentDateTime() + "-------------------------------");
        log.info("--------------------------九地市开始: " + Until.getCurrentDateTime() + "-------------------------------");
        log.info("--------九地市 年度 " + Until.getCurrentDateTime() + "------------");
        codetype = "N";
        areaType = "E";
//        listmap = getYueDuDataFromURL(Until.NIANDU_9DI, codetype, areaType);
//        saveIndexs(listmap, codetype, areaType);
//        updateIndexInfo(Until.NIANDU_9DI, codetype);
        saveNianDuData(Until.NIANDU_9DI, codetype, areaType, areaMap);
        log.info("---------九地市年度保存成功 " + Until.getCurrentDateTime());
        log.info("--------------------------九地市结束: " + Until.getCurrentDateTime() + "-------------------------------");
        log.info("--------------------------全国任务开始: " + Until.getCurrentDateTime() + "-------------------------------");
        log.info("-------全国 年度 " + Until.getCurrentDateTime() + "------------");
        codetype = "N";
        areaType = "G";
//        listmap = getYueDuDataFromURL(Until.NIANDU_QUANGUO, codetype, areaType);
//        saveIndexs(listmap, codetype, areaType);
//        updateIndexInfo(Until.NIANDU_QUANGUO, codetype);
        saveNianDuData(Until.NIANDU_QUANGUO, codetype, areaType, areaMap);
        log.info("---------全国年度保存成功 " + Until.getCurrentDateTime());
        log.info("--------------------------全国任务结束: " + Until.getCurrentDateTime() + "-------------------------------");
        log.info("--------------------------全省任务开始: " + Until.getCurrentDateTime() + "-------------------------------");
        log.info("-------全省 年度 " + Until.getCurrentDateTime() + "------------");
        codetype = "N";
        areaType = "P";
//        listmap = getYueDuDataFromURL(Until.NIANDU_QUANGUO, codetype, areaType);
//        saveIndexs(listmap, codetype, areaType);
//        updateIndexInfo(Until.NIANDU_QUANGUO, codetype);
        saveNianDuData(Until.NIANDU_QUANGUO, codetype, areaType, areaMap);
        log.info("---------全省年度保存成功 " + Until.getCurrentDateTime());
        log.info("--------------------------全国任务结束: " + Until.getCurrentDateTime() + "-------------------------------");
        log.info("--------------------------计时器结束： " + Until.getCurrentDateTime() + "-------------------------------");
    }


    /**
     * 年度区级（所有时间）
     */
    public void yearDataAllTimeA() {
        //Y月度 J 季度 N 年度
        // A 区级 B 四个特区，C 十五个副省级 D 市级数据，E 九地市  G 全国
        /*//1.更新地区
        this.savaAreaCode();*/
        //封装地区map
        List<AreaCode> areaCodeList = areaService.query();
        Map<String, String> areaMap = new HashMap<>();
        for (AreaCode areaCode : areaCodeList) {
            if (!areaMap.containsKey(areaCode.getAreaCode())) {
                areaMap.put(areaCode.getAreaCode(), areaCode.getAreaName());
            }
        }
        log.info("--------------------------区级任务开始: " + Until.getCurrentDateTime() + "-------------------------------");
        log.info("--------区级别数据 年度 " + Until.getCurrentDateTime() + "--------");
        String codetype = "N";
        String areaType = "A";
//        listmap = getYueDuDataFromURL(Until.NIANDU_QUJI, codetype, areaType);
//        saveIndexs(listmap, codetype, areaType);
//        updateIndexInfo(Until.NIANDU_QUJI, codetype);
        saveNianDuData(Until.NIANDU_QUJI, codetype, areaType, areaMap);
        log.info("---------区级别数据年度数据保存成功 " + Until.getCurrentDateTime());
        log.info("--------------------------区级任务结束: " + Until.getCurrentDateTime() + "-------------------------------");

    }

    /**
     * 年度四个经济特区（所有时间）
     */
    public void yearDataAllTimeB() {
        //Y月度 J 季度 N 年度
        // A 区级 B 四个特区，C 十五个副省级 D 市级数据，E 九地市  G 全国
        /*//1.更新地区
        this.savaAreaCode();*/
        //封装地区map
        List<AreaCode> areaCodeList = areaService.query();
        Map<String, String> areaMap = new HashMap<>();
        for (AreaCode areaCode : areaCodeList) {
            if (!areaMap.containsKey(areaCode.getAreaCode())) {
                areaMap.put(areaCode.getAreaCode(), areaCode.getAreaName());
            }
        }
        log.info("--------------------------四个经济特区任务开始: " + Until.getCurrentDateTime() + "-------------------------------");
        log.info("-------四个经济特区 年度 " + Until.getCurrentDateTime() + "------------");
        String codetype = "N";
        String areaType = "B";
//        listmap = getYueDuDataFromURL(Until.NIANDU_TEQU4, codetype, areaType);
//        saveIndexs(listmap, codetype, areaType);
//        updateIndexInfo(Until.NIANDU_TEQU4, codetype);
        saveNianDuData(Until.NIANDU_TEQU4, codetype, areaType, areaMap);
        log.info("--------------------------四个经济特区任务结束: " + Until.getCurrentDateTime() + "-------------------------------");

    }

    /**
     * 年度十五个副省级（所有时间）
     */
    public void yearDataAllTimeC() {
        //Y月度 J 季度 N 年度
        // A 区级 B 四个特区，C 十五个副省级 D 市级数据，E 九地市  G 全国
        /*//1.更新地区
        this.savaAreaCode();*/
        //封装地区map
        List<AreaCode> areaCodeList = areaService.query();
        Map<String, String> areaMap = new HashMap<>();
        for (AreaCode areaCode : areaCodeList) {
            if (!areaMap.containsKey(areaCode.getAreaCode())) {
                areaMap.put(areaCode.getAreaCode(), areaCode.getAreaName());
            }
        }
        log.info("--------------------------十五个副省级任务开始: " + Until.getCurrentDateTime() + "-------------------------------");
        log.info("---------十五个副省级 年度" + Until.getCurrentDateTime() + "-----------");
        String codetype = "N";
        String areaType = "C";
//        listmap = getYueDuDataFromURL(Until.NIANDU_FUSHENGJI15, codetype, areaType);
//        saveIndexs(listmap, codetype, areaType);
//        updateIndexInfo(Until.NIANDU_FUSHENGJI15, codetype);
        saveNianDuData(Until.NIANDU_FUSHENGJI15, codetype, areaType, areaMap);
        log.info("---------十五个副省级 年度数据保存成功 " + Until.getCurrentDateTime());
        log.info("--------------------------十五个副省级任务结束: " + Until.getCurrentDateTime() + "-------------------------------");
    }


    /**
     * 年度市级（所有时间）
     */
    public void yearDataAllTimeD() {
        log.info("--------------------------市级任务开始: " + Until.getCurrentDateTime() + "-------------------------------");
        log.info("---------市级 年度" + Until.getCurrentDateTime() + "-----------");
        String areaType = "D";
        String codetype = "N";
//        listmap = getYueDuDataFromURL(Until.NIANDU_SHIJI, codetype, areaType);
//        saveIndexs(listmap, codetype, areaType);
//        updateIndexInfo(Until.NIANDU_SHIJI, codetype);
        saveYueDuData(Until.NIANDU_SHIJI, codetype, areaType);
        log.info("---------年度数据保存成功 " + Until.getCurrentDateTime());
        log.info("--------------------------市级任务结束: " + Until.getCurrentDateTime() + "-------------------------------");
    }


    /**
     * 年度全省（所有时间）
     */
    public void yearDataAllTimeP() {
        log.info("--------------------------全省任务开始: " + Until.getCurrentDateTime() + "-------------------------------");
        log.info("---------全省 年度" + Until.getCurrentDateTime() + "-----------");
        String areaType = "P";
        String codetype = "N";
//        listmap = getYueDuDataFromURL(Until.NIANDU_QS, codetype, areaType);
//        saveIndexs(listmap, codetype, areaType);
//        updateIndexInfo(Until.NIANDU_QS, codetype);
        saveYueDuData(Until.NIANDU_QS, codetype, areaType);
        log.info("---------全省年度数据保存成功 " + Until.getCurrentDateTime());
        log.info("--------------------------全省任务结束: " + Until.getCurrentDateTime() + "-------------------------------");
    }

    /**
     * 年度九地市（所有时间）
     */
    public void yearDataAllTimeE() {
        //Y月度 J 季度 N 年度
        // A 区级 B 四个特区，C 十五个副省级 D 市级数据，E 九地市  G 全国
        /*//1.更新地区
        this.savaAreaCode();*/
        //封装地区map
        List<AreaCode> areaCodeList = areaService.query();
        Map<String, String> areaMap = new HashMap<>();
        for (AreaCode areaCode : areaCodeList) {
            if (!areaMap.containsKey(areaCode.getAreaCode())) {
                areaMap.put(areaCode.getAreaCode(), areaCode.getAreaName());
            }
        }
        log.info("--------------------------九地市开始: " + Until.getCurrentDateTime() + "-------------------------------");
        log.info("--------九地市 年度 " + Until.getCurrentDateTime() + "------------");
        String codetype = "N";
        String areaType = "E";
//        listmap = getYueDuDataFromURL(Until.NIANDU_9DI, codetype, areaType);
//        saveIndexs(listmap, codetype, areaType);
//        updateIndexInfo(Until.NIANDU_9DI, codetype);
        saveNianDuData(Until.NIANDU_9DI, codetype, areaType, areaMap);
        log.info("---------九地市年度保存成功 " + Until.getCurrentDateTime());
        log.info("--------------------------九地市结束: " + Until.getCurrentDateTime() + "-------------------------------");
    }


    /**
     * 年度全国（所有时间）
     */
    public void yearDataAllTimeG() {
        //Y月度 J 季度 N 年度
        // A 区级 B 四个特区，C 十五个副省级 D 市级数据，E 九地市  G 全国
        /*//1.更新地区
        this.savaAreaCode();*/
        //封装地区map
        List<AreaCode> areaCodeList = areaService.query();
        Map<String, String> areaMap = new HashMap<>();
        for (AreaCode areaCode : areaCodeList) {
            if (!areaMap.containsKey(areaCode.getAreaCode())) {
                areaMap.put(areaCode.getAreaCode(), areaCode.getAreaName());
            }
        }
        log.info("--------------------------全国任务开始: " + Until.getCurrentDateTime() + "-------------------------------");
        log.info("-------全国 年度 " + Until.getCurrentDateTime() + "------------");
        String codetype = "N";
        String areaType = "G";
//        listmap = getYueDuDataFromURL(Until.NIANDU_QUANGUO, codetype, areaType);
//        saveIndexs(listmap, codetype, areaType);
//        updateIndexInfo(Until.NIANDU_QUANGUO, codetype);
        saveNianDuData(Until.NIANDU_QUANGUO, codetype, areaType, areaMap);
        log.info("---------全国年度保存成功 " + Until.getCurrentDateTime());
        log.info("--------------------------全国任务结束: " + Until.getCurrentDateTime() + "-------------------------------");
    }

    public void saveYueDuDataAllYear() {
/*        log.info("--------------------------市级任务开始: " + Until.getCurrentDateTime() + "-------------------------------");
//      市级月度可以
        String codetype = "Y";
        String areaType = "D";
//        listmap = getYueDuDataFromURL(Until.YUEDU_SHIJI, codetype, areaType);
//        saveIndexs(listmap, codetype, areaType);
//        updateIndexInfo(Until.YUEDU_SHIJI,codetype);
//        saveYueDuData(Until.YUEDU_SHIJI, codetype, areaType);
        getDataService.saveYueDuDataAllYear(Until.YUEDU_SHIJI, codetype, areaType);
        log.info("---------------月度数据保存成功 " + Until.getCurrentDateTime() + "----------");*/
    }

    /**
     * 全省数据月度（所有时间）
     */
    public void saveQsDataAllYear() {
        log.info("--------------------------全省数据任务开始: " + Until.getCurrentDateTime() + "-------------------------------");
//      市级月度可以
        String codetype = "Y";
        String areaType = "P";
//        listmap = getYueDuDataFromURL(Until.YUEDU_QS, codetype, areaType);
//        saveIndexs(listmap, codetype, areaType);
//        updateIndexInfo(Until.YUEDU_QS,codetype);
//        saveYueDuData(Until.YUEDU_QS, codetype, areaType);
        getDataService.saveYueDuDataAllYear(Until.YUEDU_QS, codetype, areaType);
        log.info("---------------全省数据保存成功 " + Until.getCurrentDateTime() + "----------");
    }

    /**
     * 保存年度数据。 带地区。(调度任务)
     *
     * @param db
     * @param codetype
     */
    public void saveNianDuData(String db, String codetype, String areaType, Map<String, String> areaMap) {

        List<OneIndex> index2s = oneService.getTwoNode(codetype, "2", areaType); //获取所有二级指标
        //全国月度有四级指标
        if ("N".equals(codetype) || "Y".equals(codetype) && "G".equals(areaType)) {
            List<OneIndex> oneIndices = oneService.getTwoNode(codetype, "3", areaType); //获取所有三级指标
            index2s.addAll(oneIndices);
        }
        List<String> timeTypelist = Until.TimeTypes();
        String yearresponse = null;
        try {
            yearresponse = Until.HttpGET(Until.GET_DATA_TIME + "db=" + db, 65000, 65000);
        } catch (IOException e) {
            e.printStackTrace();
            log.info("接口：" + Until.GET_DATA_TIME + "db=" + db);
        }
        List<String> yearcodes = new ArrayList<>();
        JSONObject obj = JSONObject.parseObject(yearresponse);
        if (200 == obj.getIntValue("returncode")) {
            String year = "";
            JSONArray array = obj.getJSONArray("returndata");
            if (array.size() > 0) {
                for (int i = 0; i < array.size(); i++) {
                    year = ((JSONObject) array.get(i)).getString("code");
                    year = year.substring(0, 4);
                    yearcodes.add(year);
                }
            }
        }
        yearcodes = removeDuplicate(yearcodes);
//        List<String> yearList = yearcodes;                                        //所有年份
        List<String> yearList = Collections.singletonList(yearcodes.get(0));    //最新一年  2017
        for (String yearcode : yearList) {
            log.info("-------第" + yearcode + "--------------");
            JSONObject childObject = null;
            List<List<IndexData>> savedata = new ArrayList<>();
            Map<String, Object> map = new HashMap<>();
            Map<String, Object> map2 = new HashMap<>();
            List<AreaCode> areas = getArea(db, areaType);
            for (OneIndex item2 : index2s) {
                for (AreaCode areaitem : areas) {
                    List<IndexData> LeijiList = new ArrayList<>();
                    List<IndexData> dangqianList = new ArrayList<>();
                    List<IndexData> ratioList = new ArrayList<>();
                    List<IndexData> qimoList = new ArrayList<>();
                    List<IndexData> tongbiList = new ArrayList<>();
                    for (int i = 0; i < timeTypelist.size(); i++) //当期值，累计值，增速
                    {
                        String typecode = timeTypelist.get(i);
                        String childResponse = null;
                        try {
                            //app9snd	A09	    人口	            人口
                            //app9snd	A0901	常住人口	        常住人口
                            //appqjnd	A020C	人均地区生产总值	人均地区生产总值

                            //appfsjnd	A09	    人口	    人口
                            //appfsjnd	A0901	常住人口	常住人口
                            /*if ((("N".equals(codetype) && "E".equals(areaType)
                                    && ("A09".equals(item2.getCodeId()) || "A0901".equals(item2.getCodeId()))))
                            ||(("N".equals(codetype) && "A".equals(areaType)
                                    && "A020C".equals(item2.getCodeId())))
                            ||(("N".equals(codetype) && "C".equals(areaType)
                                    && ("A09".equals(item2.getCodeId()) || "A0901".equals(item2.getCodeId()))))) {
                                log.info( areaType + "年度指标" + item2.getCodeId() + "，第" + yearcode + "年");
                            } else {
                                continue;
                            }*/

                            childResponse = Until.HttpGET(Until.DATA_QUERY_URL_DB_URL + "db=" + db + "&zb=" + item2.getCodeId() + "&sj=" + yearcode + "&tt=" + typecode + "&reg=" + areaitem.getAreaCode(), 65000, 65000);
                        } catch (IOException e) {
                            e.printStackTrace();
                            log.info("接口：" + Until.DATA_QUERY_URL_DB_URL + "db=" + db + "&zb=" + item2.getCodeId() + "&sj=" + yearcode + "&tt=" + typecode + "&reg=" + areaitem.getAreaCode());
                            continue;
                        }
                        childObject = JSONObject.parseObject(childResponse);
                        int returnCode = childObject.getIntValue("returncode");
                        if (returnCode == 200) {
                            JSONArray childArry = childObject.getJSONObject("returndata").getJSONArray("datanodes");
                            if (childArry.size() > 0) {
                                for (int j = 0; j < childArry.size(); j++) {
                                    IndexData datas = new IndexData();
                                    JSONObject datanodes = (JSONObject) childArry.get(j);
                                    String codes = datanodes.getString("code");
                                    CodeDataBean bean = Until.spitString(codes);
                                    datas.setDatetime(bean.getDatetime());
                                    datas.setCodeId(bean.getCodeId());
                                    datas.setAreaType(areaType);
                                    datas.setAreaCode(bean.getAreaCode() == null ? "GX350200" : bean.getAreaCode());
                                    datas.setAreaName(areaMap.get(datas.getAreaCode()));
                                    datas.setCodeType(codetype);
                                    JSONObject data = datanodes.getJSONObject("data");
                                    String dataValue = data.getString("data");

                                    if (Until.DANGQIZHI.equals(typecode)) {
                                        datas.setCurrentValue(ObjectUtils.isNotBlank(dataValue) ? dataValue : "0");//当期值
                                        dangqianList.add(datas);
                                    }

                                    if (Until.LEIJIZHI.equals(typecode)) {
                                        datas.setCumulativeValue(ObjectUtils.isNotBlank(dataValue) ? dataValue : "0");//累计值
                                        LeijiList.add(datas); //所有当前值
                                    }
                                    if (Until.LEIJIZENGSU.equals(typecode)) {
                                        datas.setCumulativeRatio(ObjectUtils.isNotBlank(dataValue) ? dataValue : "0");//累计增速
                                        ratioList.add(datas);
                                    }
                                    if (Until.QIMOZHI.equals(typecode)) {
                                        datas.setQimoValue(ObjectUtils.isNotBlank(dataValue) ? dataValue : "0");//期末值
                                        qimoList.add(datas);
                                    }

                                    if (Until.TONGBIZZ.equals(typecode)) {
                                        datas.setTongbiValue(ObjectUtils.isNotBlank(dataValue) ? dataValue : "0");//比上年同期增长率
                                        tongbiList.add(datas);
                                    }

                                }
                            }
                        } else {
                            map.put(item2.getCodeId(), item2.getCodeId());
                            log.info("未处理的code: " + item2.getCodeId());
                        }
                    }
                    List<List<IndexData>> tempList = Until.remainList(dangqianList, LeijiList, ratioList);
                    if (tempList.size() == 1) {
                        savedata.add(tempList.get(0));
                    } else if (tempList.size() == 2) {
                        List<IndexData> data1 = tempList.get(0);
                        List<IndexData> data2 = tempList.get(1);
                        for (IndexData one : data1) {
                            for (IndexData two : data2) {
                                if (one.getCodeId().equals(two.getCodeId()) && one.getDatetime().equals(two.getDatetime()) && one.getAreaCode().equals(two.getAreaCode())) {
                                    one.setCumulativeValue(two.getCumulativeValue());
                                    continue;
                                }
                            }
                        }
                        savedata.add(data1);
                    } else if (tempList.size() == 3) {
                        for (IndexData one : dangqianList) {
                            for (IndexData two : LeijiList) {
                                if (one.getCodeId().equals(two.getCodeId()) && one.getDatetime().equals(two.getDatetime()) && one.getAreaCode().equals(two.getAreaCode())) {
                                    one.setCumulativeValue(two.getCumulativeValue());
                                    continue;
                                }
                            }
                        }
                        for (IndexData one : dangqianList) {
                            for (IndexData two : ratioList) {
                                if (one.getCodeId().equals(two.getCodeId()) && one.getDatetime().equals(two.getDatetime()) && one.getAreaCode().equals(two.getAreaCode())) {
                                    one.setCumulativeRatio(two.getCumulativeRatio());
                                    continue;
                                }
                            }
                        }

                        for (IndexData one : dangqianList) {
                            if ("0".equals(one.getCurrentValue()) && qimoList.size() > 0) {
                                for (IndexData two : qimoList) {
                                    if (!"0".equals(two.getQimoValue()) && one.getCodeId().equals(two.getCodeId()) && one.getDatetime().equals(two.getDatetime()) && one.getAreaCode().equals(two.getAreaCode())) {
                                        one.setCurrentValue(two.getQimoValue());
                                        continue;
                                    }
                                }
                            }
                        }
                        for (IndexData one : dangqianList) {
                            if ("0".equals(one.getCumulativeValue()) && qimoList.size() > 0) {
                                for (IndexData two : qimoList) {
                                    if (!"0".equals(two.getQimoValue()) && one.getCodeId().equals(two.getCodeId()) && one.getDatetime().equals(two.getDatetime()) && one.getAreaCode().equals(two.getAreaCode())) {
                                        one.setCumulativeValue(two.getQimoValue());
                                        continue;
                                    }
                                }
                            }
                        }

                        for (IndexData one : dangqianList) {
                            if ("0".equals(one.getCumulativeRatio()) && tongbiList.size() > 0) {
                                for (IndexData two : tongbiList) {
                                    if (!"0".equals(two.getTongbiValue()) && one.getCodeId().equals(two.getCodeId()) && one.getDatetime().equals(two.getDatetime()) && one.getAreaCode().equals(two.getAreaCode())) {
                                        one.setCumulativeRatio(two.getTongbiValue());
                                        continue;
                                    }
                                }
                            }
                        }
                        savedata.add(dangqianList);
                    }
                }
            }
            deleteDatas(yearcode, codetype, areaType);//删除历史数据
            Map<String, IndexData> resMap = new HashMap<>();
            for (List<IndexData> first : savedata) {
                for (IndexData two : first) {
                    if (!resMap.containsKey(two.getCodeId() + "#" + two.getDatetime() + "#" + two.getAreaCode())) {
                        resMap.put(two.getCodeId() + "#" + two.getDatetime() + "#" + two.getAreaCode(), two);
                    } else {
                        continue;
                    }
                }
            }
            Collection<IndexData> valueCollection = resMap.values();
            List<IndexData> valueList = new ArrayList<>(valueCollection);
            indexservice.batchSave(valueList);
            //处理三级数据
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                List<OneIndex> remains = oneService.getChileNode(entry.getValue().toString(), codetype, areaType);
                if (ObjectUtils.isNotBlank(remains) && remains.size() > 0) {
                    saveNotcodeData(remains, db, timeTypelist, yearcode, codetype, areas, areaType, resMap);
                }
            }
            //   indexservice.deletRepeatData(codetype, areaType);
        }

    }

    /**
     * 保存月度数据
     */
    public void saveYueDuData(String db, String codetype, String areaType) {
        Map<String, String> exceptonCode = new HashMap<>();
        List<OneIndex> index2s = oneService.getTwoNode(codetype, "2", areaType); //获取所有二级指标
        if ("N".equals(codetype) && "D".equals(areaType)) {
            List<OneIndex> oneIndices = oneService.getTwoNode(codetype, "4", areaType); //获取所有四级指标
            index2s.addAll(oneIndices);
        }
        List<String> timeTypelist = null;
        String yearresponse = null;
        try {
            yearresponse = Until.HttpGET(Until.GET_DATA_TIME + "db=" + db, 15000, 15000);
        } catch (IOException e) {
            e.printStackTrace();
            log.info("接口：" + Until.GET_DATA_TIME + "db=" + db);
        }

        List<String> yearcodes = new ArrayList<>();
        JSONObject obj = JSONObject.parseObject(yearresponse);
        if (200 == obj.getIntValue("returncode")) {
            String year = "";
            JSONArray array = obj.getJSONArray("returndata");
            if (array.size() > 0) {
                for (int i = 0; i < array.size(); i++) {
                    year = ((JSONObject) array.get(i)).getString("code");
                    year = year.substring(0, 4);
                    yearcodes.add(year);
                }
            }
        }
        yearcodes = removeDuplicate(yearcodes);
//        List<String> yearList = yearcodes;                                        //所有年份
        List<String> yearList = Collections.singletonList(yearcodes.get(0));        //最新一年
        timeTypelist = Until.TimeTypes();
        for (String yearcode : yearList) {
            log.info("-------第" + yearcode + "--------------");
            JSONObject childObject = null;
            List<List<IndexData>> savedata = new ArrayList<>();
            Map<String, Object> map = new HashMap<>();
            Map<String, Object> map2 = new HashMap<>();
            for (OneIndex item2 : index2s) {
                List<IndexData> LeijiList = new ArrayList<>();
                List<IndexData> dangqianList = new ArrayList<>();
                List<IndexData> ratioList = new ArrayList<>();
                List<IndexData> qimoList = new ArrayList<>();
                List<IndexData> tongbiList = new ArrayList<>();

                for (int i = 0; i < timeTypelist.size(); i++) //当期值，累计值，增速，期末值
                {
                    String typecode = timeTypelist.get(i);
                    //获取厦门月度数据
                    String childResponse = null;
                    try {
                        childResponse = Until.HttpGET(Until.DATA_QUERY_URL_DB_URL + "db=" + db + "&zb=" + item2.getCodeId() + "&sj=" + yearcode + "&tt=" + typecode, 12000, 12000);
                    } catch (IOException e) {
                        e.printStackTrace();
                        exceptonCode.put(item2.getCodeId(), item2.getCodeId());
                        log.info("接口： " + Until.DATA_QUERY_URL_DB_URL + "db=" + db + "&zb=" + item2.getCodeId() + "&sj=" + yearcode + "&tt=" + typecode);
                        map.put(item2.getCodeId(), item2.getCodeId());
                        log.info("未处理的code: " + item2.getCodeId());
                        continue;
                    }
                    childObject = JSONObject.parseObject(childResponse);
                    int returnCode = childObject.getIntValue("returncode");
                    if (returnCode == 200) {
                        JSONArray childArry = childObject.getJSONObject("returndata").getJSONArray("datanodes");
                        if (childArry.size() > 0) {
                            for (int j = 0; j < childArry.size(); j++) {

                                IndexData datas = new IndexData();
                                JSONObject datanodes = (JSONObject) childArry.get(j);
                                String codes = datanodes.getString("code");
                                CodeDataBean bean = Until.spitString(codes);
                                datas.setCodeId(bean.getCodeId());
                                datas.setDatetime(bean.getDatetime());
                                datas.setAreaCode(bean.getAreaCode() == null ? "GX350200" : bean.getAreaCode());
                                datas.setCodeType(codetype);
                                datas.setAreaType(areaType);
                                JSONObject data = datanodes.getJSONObject("data");
                                String dataValue = data.getString("data");

                                if (Until.DANGQIZHI.equals(typecode)) {
                                    datas.setCurrentValue(ObjectUtils.isNotBlank(dataValue) ? dataValue : "0");//当期值
                                    dangqianList.add(datas);
                                }

                                if (Until.LEIJIZHI.equals(typecode)) {
                                    datas.setCumulativeValue(ObjectUtils.isNotBlank(dataValue) ? dataValue : "0");//累计值
                                    LeijiList.add(datas); //所有当前值
                                }
                                if (Until.LEIJIZENGSU.equals(typecode)) {
                                    datas.setCumulativeRatio(ObjectUtils.isNotBlank(dataValue) ? dataValue : "0");//累计增速
                                    ratioList.add(datas);
                                }
                                if (Until.QIMOZHI.equals(typecode)) {
                                    datas.setQimoValue(ObjectUtils.isNotBlank(dataValue) ? dataValue : "0");//期末值
                                    qimoList.add(datas);
                                }

                                if (Until.TONGBIZZ.equals(typecode)) {
                                    datas.setTongbiValue(ObjectUtils.isNotBlank(dataValue) ? dataValue : "0");//比上年同期增长率
                                    tongbiList.add(datas);
                                }

                            }
                        }
                    } else {
                        map.put(item2.getCodeId(), item2.getCodeId());
                        log.info("未处理的code: " + item2.getCodeId());
                    }
                }
                List<List<IndexData>> tempList = Until.remainList(dangqianList, LeijiList, ratioList);
                if (tempList.size() == 1) {
                    savedata.add(tempList.get(0));
                } else if (tempList.size() == 2) {
                    List<IndexData> data1 = tempList.get(0);
                    List<IndexData> data2 = tempList.get(1);
                    for (IndexData one : data1) {
                        for (IndexData two : data2) {
                            if (one.getCodeId().equals(two.getCodeId()) && one.getDatetime().equals(two.getDatetime())) {
                                one.setCumulativeValue(two.getCumulativeValue());
                                continue;
                            }
                        }
                    }
                    savedata.add(data1);
                } else if (tempList.size() == 3) {
                    for (IndexData one : dangqianList) {
                        for (IndexData two : LeijiList) {
                            if (one.getCodeId().equals(two.getCodeId()) && one.getDatetime().equals(two.getDatetime())) {
                                one.setCumulativeValue(two.getCumulativeValue());
                                continue;
                            }
                        }
                    }
                    for (IndexData one : dangqianList) {
                        for (IndexData two : ratioList) {
                            if (one.getCodeId().equals(two.getCodeId()) && one.getDatetime().equals(two.getDatetime())) {
                                one.setCumulativeRatio(two.getCumulativeRatio());
                                continue;
                            }
                        }
                    }
                    for (IndexData one : dangqianList) {
                        if ("0".equals(one.getCurrentValue()) && qimoList.size() > 0) {
                            for (IndexData two : qimoList) {
                                if (one.getCodeId().equals(two.getCodeId()) && one.getDatetime().equals(two.getDatetime())) {
                                    one.setCurrentValue(two.getQimoValue());
                                    continue;
                                }
                            }
                        }
                    }

                    for (IndexData one : dangqianList) {
                        if ("0".equals(one.getCumulativeRatio()) && tongbiList.size() > 0) {
                            for (IndexData two : tongbiList) {
                                if (one.getCodeId().equals(two.getCodeId()) && one.getDatetime().equals(two.getDatetime())) {
                                    one.setCumulativeRatio(two.getTongbiValue());
                                    continue;
                                }
                            }
                        }
                    }
                    savedata.add(dangqianList);
                }

            }   //指标 for
            deleteDatas(yearcode, codetype, areaType); //删除历史数据 Y J
            Map<String, IndexData> resMap = new HashMap<>();
            for (List<IndexData> first : savedata) {
                for (IndexData two : first) {
                    if (!resMap.containsKey(two.getCodeId() + "#" + two.getDatetime())) {
                        resMap.put(two.getCodeId() + "#" + two.getDatetime(), two);
                    } else {
                        continue;
                    }
                }
            }
            Collection<IndexData> valueCollection = resMap.values();
            List<IndexData> valueList = new ArrayList<>(valueCollection);
            indexservice.batchSave(valueList);

            //处理三级数据
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                List<OneIndex> remains = oneService.getChileNode(entry.getValue().toString(), codetype, areaType);
                if (ObjectUtils.isNotBlank(remains) && remains.size() > 0) {
                    saveNotcodeData(remains, db, timeTypelist, yearcode, codetype, areaType, resMap);
                }
            }
//             indexservice.deletRepeatData(codetype, areaType);
        }
    }

    //**父节点**
    public List getArea(String db, String areaType) {
        JSONArray array = null;
        JSONObject areaObject = null;
        List<AreaCode> arealist = new ArrayList<>();

        if (JIDU_QUANGUO.equals(db) || NIANDU_QUANGUO.equals(db) || YUEDU_QUANGUO.equals(db)) {
            AreaCode areas = new AreaCode();
            areas.setAreaCode("GX000000");
            areas.setAreaName("全国");
            areas.setAreaType(areaType);
            arealist.add(areas);
            return arealist;
        }

        String response = null;
        try {
            response = Until.HttpGET(Until.AREA_CODE_URL_DB_URL + "db=" + db, 12000, 6000);
        } catch (IOException e) {
            e.printStackTrace();
            log.info("接口：" + Until.AREA_CODE_URL_DB_URL + "db=" + db);
        }
        JSONObject jsonObject = JSONObject.parseObject(response);
        int returncode = jsonObject.getIntValue("returncode");
        if (returncode == 200) {
            array = jsonObject.getJSONArray("returndata");
            for (int i = 0; i < array.size(); i++) {
                AreaCode areas = new AreaCode();
                areaObject = (JSONObject) array.get(i);
                areas.setAreaCode(areaObject.getString("code"));
                areas.setAreaName(areaObject.getString("name"));
                areas.setAreaType(areaType);
                arealist.add(areas);
            }
        }
        return arealist;

    }


    /**
     * 删除数据 不带codeId
     *
     * @param yearcode
     * @param codetype
     * @param areatype
     */
    public void deleteDatas(String yearcode, String codetype, String areatype) {

        indexservice.deleteIndexData(yearcode, codetype, areatype);
    }

    /**
     * 参数带地区列表  带城市
     *
     * @param list
     * @param dataBase
     * @param timetype
     * @param year
     * @param codetype
     * @param arealist
     */
    public void saveNotcodeData(List<OneIndex> list,
                                String dataBase,
                                List<String> timetype,
                                String year,
                                String codetype,
                                List<AreaCode> arealist,
                                String areaType,
                                Map<String, IndexData> resMap) {
        JSONObject childObject;
        List<List<IndexData>> savedata = new ArrayList<>();
        for (OneIndex item : list) {
            for (AreaCode areaItem : arealist) {
                List<IndexData> LeijiList = new ArrayList<>();
                List<IndexData> dangqianList = new ArrayList<>();
                List<IndexData> ratioList = new ArrayList<>();
                List<IndexData> qimoList = new ArrayList<>();

                for (String typecode : timetype) {
                    String response = null;
                    try {
                        response = Until.HttpGET(Until.DATA_QUERY_URL_DB_URL + "db=" + dataBase + "&zb=" + item.getCodeId() + "&sj=" + year + "&tt=" + typecode + "&reg=" + areaItem.getAreaCode(), 65000, 65000);
                    } catch (IOException e) {
                        e.printStackTrace();
                        log.info("接口： " + Until.DATA_QUERY_URL_DB_URL + "db=" + dataBase + "&zb=" + item.getCodeId() + "&sj=" + year + "&tt=" + typecode + "&reg=" + areaItem.getAreaCode());
                        continue;
                    }
                    childObject = JSONObject.parseObject(response);
                    int returncode = childObject.getIntValue("returncode");
                    if (200 == returncode) {
                        JSONArray childArry = childObject.getJSONObject("returndata").getJSONArray("datanodes");
                        if (childArry.size() > 0) {
                            for (int j = 0; j < childArry.size(); j++) {

                                IndexData datas4 = new IndexData();
                                JSONObject datanodes1 = (JSONObject) childArry.get(j);
                                String codes = datanodes1.getString("code");
                                CodeDataBean bean = Until.spitString(codes);
                                datas4.setCodeId(bean.getCodeId());
                                datas4.setDatetime(bean.getDatetime());
                                datas4.setAreaType(areaType);
                                datas4.setAreaCode(bean.getAreaCode() == null ? "GX350200" : bean.getAreaCode());
                                datas4.setCodeType(codetype);
                                JSONObject data = datanodes1.getJSONObject("data");
                                String dataValue = data.getString("data");

                                if (Until.DANGQIZHI.equals(typecode)) {
                                    datas4.setCurrentValue(ObjectUtils.isNotBlank(dataValue) ? dataValue : "0");//当期值
                                    dangqianList.add(datas4);
                                }

                                if (Until.LEIJIZHI.equals(typecode)) {
                                    datas4.setCumulativeValue(ObjectUtils.isNotBlank(dataValue) ? dataValue : "0");//累计值
                                    LeijiList.add(datas4); //所有当前值
                                }
                                if (Until.LEIJIZENGSU.equals(typecode)) {
                                    datas4.setCumulativeRatio(ObjectUtils.isNotBlank(dataValue) ? dataValue : "0");//累计增速
                                    ratioList.add(datas4);
                                }
                                if (Until.QIMOZHI.equals(typecode)) {
                                    datas4.setQimoValue(ObjectUtils.isNotBlank(dataValue) ? dataValue : "0");//期末值
                                    qimoList.add(datas4);
                                }

                            }
                        }
                    }
                }
                List<List<IndexData>> tempList = Until.remainList(dangqianList, LeijiList, ratioList);
                if (tempList.size() == 1) {
                    savedata.add(tempList.get(0));
                } else if (tempList.size() == 2) {
                    List<IndexData> data1 = tempList.get(0);
                    List<IndexData> data2 = tempList.get(1);
                    for (IndexData one : data1) {
                        for (IndexData two : data2) {
                            if (one.getCodeId().equals(two.getCodeId()) && one.getDatetime().equals(two.getDatetime())) {
                                one.setCumulativeValue(two.getCumulativeValue());
                                continue;
                            }
                        }
                    }
                    savedata.add(data1);

                } else if (tempList.size() == 3) {
                    for (IndexData one : dangqianList) {
                        for (IndexData two : LeijiList) {
                            if (one.getCodeId().equals(two.getCodeId()) && one.getDatetime().equals(two.getDatetime())) {
                                one.setCumulativeValue(two.getCumulativeValue());
                                continue;
                            }
                        }
                    }
                    for (IndexData one : dangqianList) {
                        for (IndexData two : ratioList) {
                            if (one.getCodeId().equals(two.getCodeId()) && one.getDatetime().equals(two.getDatetime())) {
                                one.setCumulativeRatio(two.getCumulativeRatio());
                                continue;
                            }
                        }
                    }
                    for (IndexData one : dangqianList) {
                        if ("0".equals(one.getCurrentValue()) && qimoList.size() > 0) {
                            for (IndexData two : qimoList) {
                                if (one.getCodeId().equals(two.getCodeId()) && one.getDatetime().equals(two.getDatetime())) {
                                    one.setCurrentValue(two.getQimoValue());
                                    continue;
                                }
                            }
                        } else {
                            break;
                        }
                    }
                    savedata.add(dangqianList);
                }
            }
            Map<String, IndexData> resMap1 = new HashMap<>();
            for (List<IndexData> first : savedata) {
                for (IndexData two : first) {
                    if (!resMap.containsKey(two.getCodeId() + "#" + two.getDatetime() + "#" + two.getAreaCode())) {
                        resMap.put(two.getCodeId() + "#" + two.getDatetime() + "#" + two.getAreaCode(), two);
                        resMap1.put(two.getCodeId() + "#" + two.getDatetime() + "#" + two.getAreaCode(), two);
                    } else {
                        continue;
                    }
                }
            }
            Collection<IndexData> valueCollection = resMap1.values();
            List<IndexData> valueList = new ArrayList<>(valueCollection);
            indexservice.batchSave(valueList);
        }
    }

    /**
     * 处理三级数据 月度，季度 （年度另外处理） 参数不带地区。默认厦门市
     *
     * @param list
     * @param dataBase
     * @param timetype
     * @param year
     */
    public void saveNotcodeData(List<OneIndex> list, String dataBase,
                                List<String> timetype, String year,
                                String codetype, String areaType,
                                Map<String, IndexData> resMap) {
        JSONObject childObject = null;
        List<List<IndexData>> savedata = new ArrayList<>();
        Map<String, String> exCode2 = new HashMap<>();
        for (OneIndex item : list) {
            List<IndexData> LeijiList = new ArrayList<>();
            List<IndexData> dangqianList = new ArrayList<>();
            List<IndexData> ratioList = new ArrayList<>();
            List<IndexData> qimoList = new ArrayList<>();
            for (String typecode : timetype) {
                String response;
                try {
                    response = Until.HttpGET(Until.DATA_QUERY_URL_DB_URL + "db=" + dataBase + "&zb=" + item.getCodeId() + "&sj=" + year + "&tt=" + typecode, 12000, 12000);
                } catch (IOException e) {
                    e.printStackTrace();
                    exCode2.put(item.getCodeId(), item.getCodeId());
                    log.info("接口： " + Until.DATA_QUERY_URL_DB_URL + "db=" + dataBase + "&zb=" + item.getCodeId() + "&sj=" + year + "&tt=" + typecode);
                    continue;
                }
                childObject = JSONObject.parseObject(response);
                int returncode = childObject.getIntValue("returncode");
                if (200 == returncode) {
                    JSONArray childArry = childObject.getJSONObject("returndata").getJSONArray("datanodes");
                    if (childArry.size() > 0) {
                        for (int j = 0; j < childArry.size(); j++) {

                            IndexData datas4 = new IndexData();
                            JSONObject datanodes1 = (JSONObject) childArry.get(j);
                            String codes = datanodes1.getString("code");
                            CodeDataBean bean = Until.spitString(codes);
                            datas4.setCodeId(bean.getCodeId());
                            datas4.setAreaType(areaType);
                            datas4.setDatetime(bean.getDatetime());
                            datas4.setAreaCode(bean.getAreaCode() == null ? "GX350200" : bean.getAreaCode());
                            datas4.setCodeType(codetype);
                            JSONObject data = datanodes1.getJSONObject("data");
                            String dataValue = data.getString("data");

                            if (Until.LEIJIZHI.equals(typecode)) {
                                datas4.setCumulativeValue(dataValue);//累计值
                                LeijiList.add(datas4); //所有当前值
                            }

                            if (Until.DANGQIZHI.equals(typecode)) {
                                datas4.setCurrentValue(dataValue);//当期值
                                dangqianList.add(datas4);
                            }


                            if (Until.LEIJIZENGSU.equals(typecode)) {
                                datas4.setCumulativeRatio(dataValue);//累计增速
                                ratioList.add(datas4);
                            }
                            if (Until.QIMOZHI.equals(typecode)) {
                                datas4.setQimoValue(dataValue);//期末值
                                qimoList.add(datas4);
                            }
                        }
                    }
                }
            }
            List<List<IndexData>> tempList = Until.remainList(dangqianList, LeijiList, ratioList);
            if (tempList.size() == 1) {
                savedata.add(tempList.get(0));
            } else if (tempList.size() == 2) {
                List<IndexData> data1 = tempList.get(0);
                List<IndexData> data2 = tempList.get(1);
                for (IndexData one : data1) {
                    for (IndexData two : data2) {
                        if (one.getCodeId().equals(two.getCodeId()) && one.getDatetime().equals(two.getDatetime())) {
                            one.setCumulativeValue(two.getCumulativeValue());
                            continue;
                        }
                    }
                }
                savedata.add(data1);

            } else if (tempList.size() == 3) {
                for (IndexData one : dangqianList) {
                    for (IndexData two : LeijiList) {
                        if (one.getCodeId().equals(two.getCodeId()) && one.getDatetime().equals(two.getDatetime())) {
                            one.setCumulativeValue(two.getCumulativeValue());
                            continue;
                        }
                    }
                }
                for (IndexData one : dangqianList) {
                    for (IndexData two : ratioList) {
                        if (one.getCodeId().equals(two.getCodeId()) && one.getDatetime().equals(two.getDatetime())) {
                            one.setCumulativeRatio(two.getCumulativeRatio());
                            continue;
                        }
                    }
                }
                for (IndexData one : dangqianList) {
                    if ("0".equals(one.getCurrentValue()) && qimoList.size() > 0) {
                        for (IndexData two : qimoList) {
                            if (one.getCodeId().equals(two.getCodeId()) && one.getDatetime().equals(two.getDatetime())) {
                                one.setCurrentValue(two.getQimoValue());
                                continue;
                            }
                        }
                    } else {
                        break;
                    }
                }
                savedata.add(dangqianList);
            }

            Map<String, IndexData> resMap1 = new HashMap<>();
            for (List<IndexData> first : savedata) {
                for (IndexData two : first) {
                    if (!resMap.containsKey(two.getCodeId() + "#" + two.getDatetime())) {
                        resMap.put(two.getCodeId() + "#" + two.getDatetime(), two);
                        resMap1.put(two.getCodeId() + "#" + two.getDatetime(), two);
                    } else {
                        continue;
                    }
                }
            }
            Collection<IndexData> valueCollection = resMap1.values();
            List<IndexData> valueList = new ArrayList<>(valueCollection);
            indexservice.batchSave(valueList);
        }

    }


    /**
     * 更新解释 和单位
     *
     * @param db
     * @param codeType
     * @return
     */
    public void updateIndexInfo(String db, String codeType) {
        JSONObject indexInfo = null;
        String areaType = "";
        if (JIDU_9DI.endsWith(db) || NIANDU_9DI.equals(db) || YUEDU_9DI.equals(db)) {
            areaType = "E";
        } else if (JIDU_FUSHENGJI15.endsWith(db) || NIANDU_FUSHENGJI15.equals(db) || YUEDU_FUSHENGJI15.equals(db)) {
            areaType = "C";
        } else if (JIDU_TEQU4.endsWith(db) || NIANDU_TEQU4.equals(db) || YUEDU_TEQU4.equals(db)) {
            areaType = "B";
        } else if (JIDU_QUANGUO.endsWith(db) || NIANDU_QUANGUO.equals(db) || YUEDU_QUANGUO.equals(db)) {
            areaType = "G";
        } else if (JIDU_QUJI.endsWith(db) || NIANDU_QUJI.equals(db) || YUEDU_QUJI.equals(db)) {
            areaType = "A";
        } else if (JIDU_SHIJI.endsWith(db) || NIANDU_SHIJI.equals(db) || YUEDU_SHIJI.equals(db)) {
            areaType = "D";
        } else {
            areaType = "S";
        }
        List<OneIndex> indexlist = oneService.getAllIndexs(codeType, areaType);

        for (OneIndex item : indexlist) {
            String response = null;
            try {
                response = Until.HttpGET(Until.GET_DATA_INFO + "&db=" + db + "&code=" + item.getCodeId(), 24000, 24000);
            } catch (IOException e) {
                e.printStackTrace();
                log.info("接口：" + Until.GET_DATA_INFO + "&db=" + db + "&code=" + item.getCodeId());
            }
            if (ObjectUtils.isBlank(response)) {
                continue;
            }
            JSONObject obj = JSONObject.parseObject(response);
            int returncode = obj.getIntValue("returncode");
            if (200 == returncode) {
                // OneIndex index    = new OneIndex();
                indexInfo = obj.getJSONObject("returndata");
                // index.setCodeId(indexInfo.getString("code"));
                item.setUnit(indexInfo.getString("unitname"));
                //indexlist.add(index);
            }
            oneService.update(item);
        }

        System.out.println("更新成功");

    }
}
